<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\OrdersImport;
use App\Admin\Repositories\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('security_name');
            $grid->column('security_code');
            $grid->column('transaction_number');
            $grid->column('transaction_amount');
            $grid->column('buy_time');
            $grid->column('average_amount');
            $grid->column('sell_transaction_number');
            $grid->column('sell_transaction_amount');
            $grid->column('sell_average_amount');
            $grid->column('sell_time');
            $grid->column('hold_day')->display(function ($var){
                if($var==0){
                    $buy_time = $this->buy_time;
                    $var = ceil((time()-strtotime($buy_time))/86400);
                }
                return $var;
            });
            $grid->column('win_lose_amount');
            $grid->column('win_lose_percent','盈亏比例')->display(function ($var){
                return $var.'%';
            })->sortable();
            $grid->column('buy_reason','买入原因')->display(function ($var){
                return  Order::BUY_REASON[$var];
            });
            $grid->column('sell_reason','卖出原因')->display(function ($var){
                return  Order::SELL_REASON[$var];
            });
            $grid->column('status','状态')->display(function ($var){
                return  Order::STATUS[$var];
            });;
            $grid->column('buy_form','买入形态')->display(function ($var){
                return  Order::BUY_FORM[$var];
            });
            $grid->column('commission_amount');
            $grid->column('stamp_amount');
            $grid->column('other_amount');
            $grid->column('remark');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('security_name');
                $filter->equal('security_code');
                $filter->equal('status','状态')->select(Order::STATUS);
                $filter->equal('is_win','是否盈利')->select(Order::IS_WIN);
                $filter->equal('sell_reason','卖出原因')->select(Order::SELL_REASON);
                $filter->between('buy_time')->date();
                $filter->between('sell_time')->date();

            });
            $grid->export()->xlsx();
            $grid->tools(function (Grid\Tools $tools) {
                //Excel导入
                $tools->append(new  OrdersImport());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('security_name');
            $show->field('security_code');
            $show->field('completion_date');
            $show->field('transaction_number');
            $show->field('transaction_amount');
            $show->field('commission_amount');
            $show->field('stamp_amount');
            $show->field('other_amount');
            $show->field('buy_time');
            $show->field('sell_time');
            $show->field('sell_transaction_number');
            $show->field('sell_average_amount');
            $show->field('sell_transaction_amount');
            $show->field('hold_day');
            $show->field('win_lose_amount');
            $show->select('buy_reason','买入原因')->options(function(){
                return  Order::BUY_REASON;
            });
            $show->select('sell_reason','卖出原因')->options(function(){
                return  Order::SELL_REASON;
            });
            $show->select('status','状态')->options(function(){
                return  Order::STATUS;
            });
            $show->select('buy_form','买入形态')->options(function(){
                return  Order::BUY_FORM;
            });
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('security_name')->readOnly();
            $form->text('security_code')->readOnly();
            $form->text('completion_date')->readOnly();
            $form->text('transaction_number')->readOnly();
            $form->text('transaction_amount')->readOnly();
            $form->text('commission_amount')->readOnly();
            $form->text('stamp_amount')->readOnly();
            $form->text('other_amount')->readOnly();
            $form->text('contract_number')->readOnly();
            $form->text('buy_time')->readOnly();
            $form->text('sell_time')->readOnly();
            $form->text('sell_transaction_number')->readOnly();
            $form->text('sell_average_amount')->readOnly();
            $form->text('sell_transaction_amount')->readOnly();
            $form->text('hold_day')->readOnly();
            $form->text('win_lose_amount')->readOnly();
            $form->select('buy_reason','买入原因')->options(function(){
                return  Order::BUY_REASON;
            });
            $form->select('sell_reason','卖出原因')->options(function(){
                return  Order::SELL_REASON;
            });
            $form->select('status','状态')->options(function(){
                return  Order::STATUS;
            });
            $form->select('buy_form','买入形态')->options(function(){
                return  Order::BUY_FORM;
            });

            $form->text('remark');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
